// ws://www.saas.sit.guoxintianyu.com/websocket/protocol/hardware
let ws: WebSocket | null = null
const baseUrl = 'ws://www.saas.sit.guoxintianyu.com'
export default {
  onopen(url: string, callback?: () => void) {
    ws = new WebSocket(`${baseUrl}${url}`)
    ws.onopen = () => {
      // console.log('连接成功！');
      callback && callback()
    }
  },
  onmessage(callback: (arg0: any) => void) {
    ws!.onmessage = (e) => {
      const redata = JSON.parse(e.data)
      callback(redata)
    }
  },
  onerror() {
    ws!.onerror = () => {}
  },
  onclose() {
    ws!.onclose = () => {
      ws!.close()
    }
  },
  send(data: any) {
    ws!.send(data)
  }
}
